﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Logic;
using Entities;

namespace Presentation
{
	public partial class TaskDialogAddMovieStep2 : Form
	{
		private List<Movie> movies = new List<Movie>();

		public List<Movie> Movies
		{
			get { return movies; }
			set { movies = value; }
		}
		
		public TaskDialogAddMovieStep2()
		{
			InitializeComponent();
		}

		public TaskDialogAddMovieStep2(List<Movie> movies)
		{
			this.movies = movies;

			InitializeComponent();
		}

		public void UpdateSelectedMovie(Movie entity)
		{
			treeViewMovies.SelectedNode.Tag = entity;
			treeViewMovies.SelectedNode.Text = entity.Title;
		}

		private void TaskDialogAddMovieStep2_Load(object sender, EventArgs e)
		{
			foreach (Movie mo in movies)
			{
				TreeNode obj = new TreeNode();
				obj.Tag = mo;
				obj.Text = mo.Title;
				obj.Checked = true;

				treeViewMovies.Nodes.Add(obj);
			}

			treeViewMovies.SelectedNode = treeViewMovies.Nodes[0];
			UpdateSelectedMovie((Movie)(movieDataEditor.Tag));
			//movieDataEditor.Initialize((Movie)(treeViewMovies.Nodes[0].Tag));
		}

		private void checkBoxSelectAll_CheckedChanged(object sender, EventArgs e)
		{
			for (int i = 0; i < treeViewMovies.Nodes.Count; i++)
				treeViewMovies.Nodes[i].Checked = checkBoxSelectAll.Checked;
		}

		private void treeViewMovies_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (treeViewMovies.SelectedNode != null)
			{
				movieDataEditor.Initialize((Movie)(treeViewMovies.SelectedNode.Tag));
			}
		}

		private void treeViewMovies_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (treeViewMovies.SelectedNode != null)
			{
				UpdateSelectedMovie((Movie)(movieDataEditor.Tag));
			}
		}

		private void buttonAdd_Click(object sender, EventArgs e)
		{
			UpdateSelectedMovie((Movie)(movieDataEditor.Tag));

			movies.Clear();
			for (int i = 0; i < treeViewMovies.Nodes.Count; i++)
			{
				if (treeViewMovies.Nodes[i].Checked)
				{
					movies.Add((Movie)(treeViewMovies.Nodes[i].Tag));
				}
			}

			DialogResult = DialogResult.OK;
		}
	}
}
